const api = require("../../utils/cloudAPIUtils");
const marketApi = require("../../utils/marketAPIUtils");
const app = getApp();

Page({
  data: {
    user:{
      memberName:'微信用户',
      memberFaceImg:'/static/img/yonghu.png'
    },
    modalName:null,
    bindPhone:false,
    hasMarket:app.globalData.HasMarket,
    phone:'',
    orderIcon:[
        {id:'0',src:'/static/ico/sho1.svg',title:'待支付',num:0},
        {id:'1',src:'/static/ico/sho2.svg',title:'待发货',num:0},
        {id:'2',src:'/static/ico/sho3.svg',title:'待收货',num:0},
        {id:'3',src:'/static/ico/sho4.svg',title:'待评价',num:0}
    ],
    hasMarket:app.globalData.HasMarket
  },

  _loadAppConfig:function(){
    const _self = this;
    api.post(api.url.commonAppConfig,{
        appId:wx.getAccountInfoSync().miniProgram.appId,
        type:1
    },res=>{
        if(res.data.code==200){
            _self.setData({phone:res.data.body.serverPhone})
        }
    })
  },

  _loadOrderCount:function(){
    const _self = this;
    marketApi.post(marketApi.url.findOrderCount,{},res=>{
        if(res.data.code == 200){
            var orderIcon = _self.data.orderIcon;
            orderIcon[0].num = res.data.body.waitPayCount;
            orderIcon[1].num = res.data.body.waitDeliverCount;
            orderIcon[2].num = res.data.body.waitReceiptCount;
            orderIcon[3].num = res.data.body.waitEvaluationCount;
            _self.setData({
                orderIcon:orderIcon
            })
        }
    })
  },

  toInfo(){
    wx.navigateTo({
      url: './info/info',
    })
  },

  butInterface(e){
    let flag = e.currentTarget.dataset.flag;
    let self = this;
    switch(flag){
      case 'wallet':
        // console.log('我的钱包');
        wx.reLaunch({
          url: "/pages/wallet/wallet",
        })
        break;
      case 'orderList' :
        // console.log('我的订单');
        wx.navigateTo({
          url: "/pages/order/index",
        })
        break;
      case 'detailAccount':
        // console.log('交易明细');
        wx.navigateTo({
          url: "/pages/wallet/list/list",
        })
        break;
      case 'phone':
        // console.log('手机注册');
        wx.navigateTo({
            url: "/pages/authorize/authorize",
        })
        break;
      case 'coupon':
        wx.navigateTo({
            url: "/pages/mine/coupon/coupon",
        })
        break;
    case 'address':
        wx.navigateTo({
            url: "/pages/mine/address/list"
        })
        break;
      case 'kefu' :
        // console.log('联系客服');
        self.setData({
          modalName:'Modal',
        })
        break;
      default:
        wx.showToast({
          title: '功能开发中 ...',
          icon: 'error',
          duration: 2000
        })
        break;
    }
  },

  //关闭客服模态框
  btnHideModal(){
    this.setData({
      modalName:null,
      bindPhone:false
    })
  },
  //拨打客服电话
  butCall(){
    let self = this;
    wx.makePhoneCall({
      phoneNumber: self.data.phone,
      complete(){
        self.btnHideModal();
      }
    })
  },

  toOrder(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/market/pages/order/list?id='+id,
    })
  },

  onLoad(options) {
    this.setData({
        phone:app.globalData.phone,
        bindPhone:app.globalData.userInfo.memberPhone?false:true
    })
    this._loadAppConfig();
    this._loadOrderCount();
  },

  onShow(){
    let info = app.globalData.userInfo;
    if(info.loginToken){
    //   if(info.loginLevel == 1){
    //     wx.navigateTo({
    //       url: '/pages/authorize/authorize',
    //     })
    //   }
      this.setData({user:info})
    }else{
        wx.showModal({
          title: '登录失败',
          content: '请联系客服人员！',
          showCancel:false
        })
    }
  },
})